/**
 * @author erond
 */

package master;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.bluetooth.BluetoothStateException;
//import javax.swing.JOptionPane;
//import javax.swing.JDialog;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;

import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import com.intel.bluetooth.BlueCoveImpl;
import commonUtility.NetworkInterfaceIP;



/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * Interfaccia grafica per il settaggio dei valori del Master al bootstrap
 */
public class MasterSetting extends javax.swing.JFrame implements Runnable {

	{
		//Set Look & Feel   
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private static final long serialVersionUID = -7200521944811645728L;
	private JPanel jPanel1;
	private JButton enableAfter;
	private JTextField afterMulticastPort;
	private JLabel jLabel29;
	private JLabel jLabel28;
	private JLabel jLabel25;
	private JLabel jLabel24;
	private JComboBox jComboBox_BluetoothInterfaces;
	private JLabel jLabel23;
	private JLabel jLabel22;
	private JLabel copyright;
	private JCheckBox jCheckReset;
	private JSeparator jSeparator2;
	private JSeparator jSeparator1;
	private JLabel jLabel21;
	private JLabel jLabel19;
	private JTextField afterMulticastIP;
	private JLabel jLabel18;
	private JTextField client_serverIP;
	private JLabel jLabel17;
	private JTextField slaveDeleteServerPort_after;
	private JLabel jLabel16;
	private JTextField server_serverInputPort;
	private JLabel jLabel15;
	private JTextField slaveMulticastPort;
	private JLabel jLabel14;
	private JTextField slaveMulticastIP;
	private JLabel jLabel13;
	private JTextField inquiryMaxPacket;
	private JLabel jLabel12;
	private JTextField inquiryMaxTime;
	private JLabel jLabel11;
	private JTextField slaveIP;
	private JLabel jLabel10;
	private JTextField masterJobPort;
	private JLabel jLabel5;
	private JLabel jLabel9;
	private JTextField beforeMulticastPort;
	private JLabel jLabel8;
	private JTextField beforeMulticastIP;
	private JLabel jLabel6;
	private JTextField server_serverIP;
	private JTextField slaveDeleteServerPort_before;
	private JLabel jLabel4;
	private JLabel jLabel7;
	private JTextField client_serverInputPort;
	private JLabel jLabel20;
	private JLabel jLabel3;
	private JLabel jLabel1;
	private JButton enableBefore;
	private JPanel jPanelGatewayClient;
	private JPanel jPanelMasterGateway;
	private JPanel jPanelGatewayServer;
	private JLabel jLabel2;
	private JButton jButtonStart;
	private JPanel jPanel3;
	private JLabel jLabelMasterLogo;
	private JPanel jPanel2;
	private boolean gatewayClientEnabled = true;
	private boolean gatewayServerEnabled = true;
	private Master master = null;
	private JComboBox jComboBox_NetworkInterfaces;
	private JTextField jTextField_DefaultFile;
	private JLabel jLabel27;
	private JTextField jTextField_InquiryThresholding;
	private JLabel jLabel26;

	public void setMaster(Master m) {
		master=m;
	}

	public Master getMaster() {
		return master;
	}

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MasterSetting inst = new MasterSetting("Master Gateway bootstrap setting",null);
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public void run() {
		MasterSetting inst = new MasterSetting("Master Gateway bootstrap setting",getMaster());
		inst.setLocationRelativeTo(null);
		inst.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public MasterSetting(String name, Master m) {
		super(name);
		initGUI();
		setMaster(m);
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jPanel1 = new JPanel();
				GroupLayout jPanel1Layout = new GroupLayout((JComponent)jPanel1);
				jPanel1.setLayout(jPanel1Layout);
				getContentPane().add(jPanel1, BorderLayout.CENTER);
				jPanel1.setPreferredSize(new java.awt.Dimension(772, 578));
				{
					jPanel2 = new JPanel();
					GroupLayout jPanel2Layout = new GroupLayout((JComponent)jPanel2);
					jPanel2.setLayout(jPanel2Layout);
					{
						ImageIcon masterLogo = new ImageIcon("./media/bradipo.png");
						jLabelMasterLogo = new JLabel();
						GroupLayout jLabelMasterLogoLayout = new GroupLayout((JComponent)jLabelMasterLogo);
						jLabelMasterLogo.setLayout(jLabelMasterLogoLayout);
						jLabelMasterLogo.setIcon(masterLogo);
						jPanel2.add(jLabelMasterLogo);
						jLabelMasterLogoLayout.setVerticalGroup(jLabelMasterLogoLayout.createSequentialGroup());
						jLabelMasterLogoLayout.setHorizontalGroup(jLabelMasterLogoLayout.createSequentialGroup());
					}

					jPanel2Layout.setHorizontalGroup(jPanel2Layout.createSequentialGroup()
						.addContainerGap(186, 186)
						.addComponent(jLabelMasterLogo, 0, 572, Short.MAX_VALUE)
						.addContainerGap());
					jPanel2Layout.setVerticalGroup(jPanel2Layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jLabelMasterLogo, 0, 88, Short.MAX_VALUE));
				}
				{
					jPanel3 = new JPanel();
					GroupLayout jPanel3Layout = new GroupLayout((JComponent)jPanel3);
					jPanel3.setLayout(jPanel3Layout);
					{
						jPanelGatewayServer = new JPanel();
						jPanelGatewayServer.setBorder(BorderFactory.createTitledBorder(""));
						{
							jLabel15 = new JLabel();
							jPanelGatewayServer.add(jLabel15);
							jLabel15.setText("Server_Input PORT");
						}
						{
							server_serverInputPort = new JTextField();
							jPanelGatewayServer.add(getServer_serverInputPort());
							server_serverInputPort.setText("21000");
						}
						{
							jLabel16 = new JLabel();
							jPanelGatewayServer.add(jLabel16);
							jLabel16.setText("Slave Delete PORT");
						}
						{
							slaveDeleteServerPort_after = new JTextField();
							jPanelGatewayServer.add(getSlaveDeleteServerPort_after());
							slaveDeleteServerPort_after.setText("30002");
						}
						{
							jLabel17 = new JLabel();
							jPanelGatewayServer.add(jLabel17);
							jLabel17.setText("After Server IP");
						}
						{
							client_serverIP = new JTextField();
							jPanelGatewayServer.add(getClient_serverIP());
							client_serverIP.setText("192.168.178.68");
						}
						{
							jLabel18 = new JLabel();
							jPanelGatewayServer.add(jLabel18);
							jLabel18.setText("After Multicast IP");
						}
						{
							afterMulticastIP = new JTextField();
							jPanelGatewayServer.add(getAfterMulticastIP());
							afterMulticastIP.setText("230.0.0.3");
						}
						{
							jLabel19 = new JLabel();
							jPanelGatewayServer.add(jLabel19);
							jLabel19.setText("After Multicast PORT");
						}
						{
							afterMulticastPort = new JTextField();
							jPanelGatewayServer.add(getAfterMulticastPort());
							jPanelGatewayServer.add(getJLabel25());
							afterMulticastPort.setText("65433");
						}
						{
							jSeparator2 = new JSeparator();
							jPanelGatewayServer.add(jSeparator2);
							jSeparator2.setPreferredSize(new java.awt.Dimension(142, 6));
						}
						{
							jLabel21 = new JLabel("ENABLED",JLabel.CENTER);
							jPanelGatewayServer.add(jLabel21);
							jPanelGatewayServer.add(getJLabel29());
							jLabel21.setFont(new java.awt.Font("AlArabiya",1,12));
							jLabel21.setForeground(new java.awt.Color(2,255,0));
							jLabel21.setPreferredSize(new java.awt.Dimension(108,29));
							jLabel21.setBorder(BorderFactory.createTitledBorder(""));
						}
						{
							enableAfter = new JButton();
							jPanelGatewayServer.add(enableAfter);
							enableAfter.setText("Enable/Disable");
							enableAfter.setPreferredSize(new java.awt.Dimension(196, 70));
							enableAfter.setToolTipText("Press to Enable/Disable the After Gateway link");
							enableAfter.setFont(new java.awt.Font("AlArabiya",2,20));
							enableAfter.addMouseListener(new MouseAdapter() {
								public void mouseClicked(MouseEvent evt) {
									enableAfterMouseClicked(evt);
								}
							});
						}
					}
					{
						jPanelMasterGateway = new JPanel();
						jPanelMasterGateway.setBorder(BorderFactory.createTitledBorder(""));
						
						{
							jLabel9 = new JLabel();
							jPanelMasterGateway.add(jLabel9);
							jLabel9.setText("Master Job PORT");
						}
						{
							masterJobPort = new JTextField();
							jPanelMasterGateway.add(getMasterJobPort());
							masterJobPort.setText("20000");
						}
						{
							jLabel10 = new JLabel();
							jPanelMasterGateway.add(jLabel10);
							jLabel10.setText("Slave IP Address");
						}
						{
							slaveIP = new JTextField();
							jPanelMasterGateway.add(slaveIP);
							slaveIP.setText("localhost");
							slaveIP.setPreferredSize(new java.awt.Dimension(108, 27));
							slaveIP.setHorizontalAlignment(SwingConstants.CENTER);
						}
						{
							jLabel11 = new JLabel();
							jPanelMasterGateway.add(jLabel11);
							jLabel11.setText("Inquiry max TIME");
						}
						{
							inquiryMaxTime = new JTextField();
							jPanelMasterGateway.add(getInquiryMaxTime());
							inquiryMaxTime.setText("20");
						}
						{
							jLabel12 = new JLabel();
							jPanelMasterGateway.add(jLabel12);
							jLabel12.setText("Inquiry max PACKET");
						}
						{
							inquiryMaxPacket = new JTextField();
							jPanelMasterGateway.add(getInquiryMaxPacket());
							jPanelMasterGateway.add(getJLabel26());
							jPanelMasterGateway.add(getJTextField_InquiryThresholding());
							inquiryMaxPacket.setText("10");
						}
						{
							jLabel13 = new JLabel();
							jPanelMasterGateway.add(jLabel13);
							jLabel13.setText("Slave Multicast IP");
						}
						{
							slaveMulticastIP = new JTextField();
							jPanelMasterGateway.add(getSlaveMulticastIP());
							slaveMulticastIP.setText("230.0.0.2");
							slaveMulticastIP.setPreferredSize(new java.awt.Dimension(81, 27));
							slaveMulticastIP.setHorizontalAlignment(SwingConstants.CENTER);
						}
						{
							jLabel14 = new JLabel();
							jPanelMasterGateway.add(jLabel14);
							jLabel14.setText("Slave Multicast PORT");
						}
						{
							slaveMulticastPort = new JTextField();
							jPanelMasterGateway.add(getSlaveMulticastPort());
							jPanelMasterGateway.add(getJLabel22());
							slaveMulticastPort.setText("65432");
						}
						{
							jPanelMasterGateway.add(getJComboBox_NetworkInterfaces());
							jPanelMasterGateway.add(getJLabel23());
							jPanelMasterGateway.add(getJComboBox_BluetoothInterfaces());
							jPanelMasterGateway.add(getJLabel27());
							jPanelMasterGateway.add(getJTextField_DefaultFile());
						}
					}
					{
						jPanelGatewayClient = new JPanel();
						jPanelGatewayClient.setBorder(BorderFactory.createTitledBorder(""));
						{
							jLabel4 = new JLabel();
							jPanelGatewayClient.add(jLabel4);
							jLabel4.setText("Server_Input PORT");
						}
						{
							client_serverInputPort = new JTextField();
							jPanelGatewayClient.add(getClient_serverInputPort());
							client_serverInputPort.setText("22000");
						}
						{
							jLabel7 = new JLabel();
							jPanelGatewayClient.add(jLabel7);
							jLabel7.setText("Slave Delete PORT");
						}
						{
							slaveDeleteServerPort_before = new JTextField();
							jPanelGatewayClient.add(getSlaveDeleteServerPort_before());
							slaveDeleteServerPort_before.setText("30001");
						}
						{
							jLabel5 = new JLabel();
							jPanelGatewayClient.add(jLabel5);
							jLabel5.setText("Before Server IP");
						}
						{
							server_serverIP = new JTextField();
							jPanelGatewayClient.add(getServer_serverIP());
							server_serverIP.setText("192.168.178.71");
						}
						{
							jLabel6 = new JLabel();
							jPanelGatewayClient.add(jLabel6);
							jLabel6.setText("Before Multicast IP");
						}
						{
							beforeMulticastIP = new JTextField();
							jPanelGatewayClient.add(getBeforeMulticastIP());
							beforeMulticastIP.setText("230.0.0.1");
						}
						{
							jLabel8 = new JLabel();
							jPanelGatewayClient.add(jLabel8);
							jLabel8.setText("Before Multicast PORT");
						}
						{
							beforeMulticastPort = new JTextField();
							jPanelGatewayClient.add(getBeforeMulticastPort());
							jPanelGatewayClient.add(getJLabel24());
							beforeMulticastPort.setText("65431");
						}
						{
							jSeparator1 = new JSeparator();
							jPanelGatewayClient.add(jSeparator1);
							jSeparator1.setPreferredSize(new java.awt.Dimension(142, 6));
						}
						{
							jLabel20 = new JLabel("ENABLED",JLabel.CENTER);
							jPanelGatewayClient.add(jLabel20);
							jPanelGatewayClient.add(getJLabel28());
							jLabel20.setFont(new java.awt.Font("AlArabiya",1,12));
							jLabel20.setForeground(new java.awt.Color(2,255,0));
							jLabel20.setBorder(BorderFactory.createTitledBorder(""));
							jLabel20.setPreferredSize(new java.awt.Dimension(108, 29));
						}
						{
							enableBefore = new JButton();
							jPanelGatewayClient.add(enableBefore);
							enableBefore.setText("Enable/Disable");
							enableBefore.setPreferredSize(new java.awt.Dimension(196, 70));
							enableBefore.setToolTipText("Press to Enable/Disable the Before Gateway link");
							enableBefore.setFont(new java.awt.Font("AlArabiya",2,20));
							enableBefore.addMouseListener(new MouseAdapter() {
								public void mouseClicked(MouseEvent evt) {
									enableBeforeMouseClicked(evt);
								}
							});
						}
					}
					jPanel3Layout.setHorizontalGroup(jPanel3Layout.createSequentialGroup()
						.addComponent(jPanelGatewayClient, GroupLayout.PREFERRED_SIZE, 253, GroupLayout.PREFERRED_SIZE)
						.addComponent(jPanelMasterGateway, GroupLayout.PREFERRED_SIZE, 249, GroupLayout.PREFERRED_SIZE)
						.addComponent(jPanelGatewayServer, GroupLayout.PREFERRED_SIZE, 254, GroupLayout.PREFERRED_SIZE));
					jPanel3Layout.setVerticalGroup(jPanel3Layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(jPanel3Layout.createParallelGroup()
						    .addComponent(jPanelGatewayClient, GroupLayout.Alignment.LEADING, 0, 331, Short.MAX_VALUE)
						    .addComponent(jPanelMasterGateway, GroupLayout.Alignment.LEADING, 0, 331, Short.MAX_VALUE)
						    .addComponent(jPanelGatewayServer, GroupLayout.Alignment.LEADING, 0, 331, Short.MAX_VALUE)));
				}
				{
					copyright = new JLabel();
					copyright.setText("Coluccio-Di Mango-Ciavarella Copyright ©2009 - all rights reserved");
					copyright.setFont(new java.awt.Font("AlArabiya",1,8));
					copyright.setEnabled(false);
					copyright.setHorizontalAlignment(SwingConstants.CENTER);
				}
				{
					jCheckReset = new JCheckBox();
					jCheckReset.setText("Autoreset");
					jCheckReset.setFont(new java.awt.Font("AlArabiya",1,10));
					jCheckReset.setToolTipText("Enabled = autoreset enable");
					jCheckReset.setEnabled(true);
					jCheckReset.setForeground(new java.awt.Color(220,2,2));
					jCheckReset.setSelected(true);
				}

				{
					jLabel1 = new JLabel();
					GroupLayout jLabel1Layout = new GroupLayout((JComponent)jLabel1);
					jLabel1.setText("Before Gateway Link");
					jLabel1.setFont(new java.awt.Font("Purisa",1,14));
					jLabel1.setLayout(null);
					jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
					jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
					jLabel1.setForeground(new java.awt.Color(0,0,255));
					jLabel1Layout.setVerticalGroup(jLabel1Layout.createSequentialGroup());
					jLabel1Layout.setHorizontalGroup(jLabel1Layout.createSequentialGroup());
				}
				{
					jLabel3 = new JLabel();
					GroupLayout jLabel3Layout = new GroupLayout((JComponent)jLabel3);
					jLabel3.setText("After Gateway Link");
					jLabel3.setFont(new java.awt.Font("Purisa",1,14));
					jLabel3.setLayout(null);
					jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
					jLabel3.setHorizontalTextPosition(SwingConstants.CENTER);
					jLabel3.setForeground(new java.awt.Color(0,0,255));
					jLabel3Layout.setVerticalGroup(jLabel3Layout.createSequentialGroup());
					jLabel3Layout.setHorizontalGroup(jLabel3Layout.createSequentialGroup());
				}

				{
					jLabel2 = new JLabel();
					GroupLayout jLabel2Layout = new GroupLayout((JComponent)jLabel2);
					jLabel2.setLayout(jLabel2Layout);
					jLabel2.setText("Master Gateway");
					jLabel2.setFont(new java.awt.Font("Purisa",1,14));
					jLabel2.setForeground(new java.awt.Color(0,0,255));
					jLabel2Layout.setVerticalGroup(jLabel2Layout.createSequentialGroup());
					jLabel2Layout.setHorizontalGroup(jLabel2Layout.createSequentialGroup());
				}
				{
					jButtonStart = new JButton();
					GroupLayout jButtonStartLayout = new GroupLayout((JComponent)jButtonStart);
					jButtonStart.setLayout(null);
					jButtonStart.setText("START");
					jButtonStart.setToolTipText("Click to start the Master!!!");
					jButtonStart.setFont(new java.awt.Font("AlArabiya",1,16));
					jButtonStart.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							jButtonStartMouseClicked(evt);
						}
					});
					jButtonStartLayout.setVerticalGroup(jButtonStartLayout.createParallelGroup());
					jButtonStartLayout.setHorizontalGroup(jButtonStartLayout.createParallelGroup());
				}
				jPanel1Layout.setHorizontalGroup(jPanel1Layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(jPanel1Layout.createParallelGroup()
					    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					        .addComponent(jPanel3, 0, 756, Short.MAX_VALUE)
					        .addGap(10))
					    .addComponent(jPanel2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 766, GroupLayout.PREFERRED_SIZE)
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addPreferredGap(jPanel3, copyright, LayoutStyle.ComponentPlacement.INDENT)
					        .addGroup(jPanel1Layout.createParallelGroup()
					            .addComponent(copyright, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 241, GroupLayout.PREFERRED_SIZE)
					            .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					                .addGap(19)
					                .addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 192, GroupLayout.PREFERRED_SIZE)
					                .addGap(30)))
					        .addGap(63)
					        .addGroup(jPanel1Layout.createParallelGroup()
					            .addComponent(jLabel2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
					            .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					                .addPreferredGap(jLabel2, jButtonStart, LayoutStyle.ComponentPlacement.INDENT)
					                .addComponent(jButtonStart, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)
					                .addGap(9)))
					        .addGap(69)
					        .addGroup(jPanel1Layout.createParallelGroup()
					            .addGroup(jPanel1Layout.createSequentialGroup()
					                .addComponent(jLabel3, GroupLayout.PREFERRED_SIZE, 215, GroupLayout.PREFERRED_SIZE)
					                .addGap(0, 0, Short.MAX_VALUE))
					            .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					                .addGap(103)
					                .addComponent(jCheckReset, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
					                .addGap(0, 26, Short.MAX_VALUE)))
					        .addGap(32))));
				jPanel1Layout.setVerticalGroup(jPanel1Layout.createSequentialGroup()
					.addContainerGap()
					.addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(jPanel1Layout.createParallelGroup()
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addGap(0, 0, Short.MAX_VALUE)
					        .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					            .addComponent(jLabel3, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
					            .addComponent(jLabel1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)))
					    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, Short.MAX_VALUE)
					        .addComponent(jLabel2, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)))
					.addComponent(jPanel3, GroupLayout.PREFERRED_SIZE, 343, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, GroupLayout.PREFERRED_SIZE)
					.addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(jButtonStart, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
					    .addComponent(jCheckReset, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE)
					    .addComponent(copyright, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE))
					.addContainerGap());
			}
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public JButton getJButtonStart() {
		return jButtonStart;
	}

	public JPanel getJPanelGatewayServer() {
		return jPanelGatewayServer;
	}

	public JPanel getJPanelMasterGateway() {
		return jPanelMasterGateway;
	}

	public JPanel getJPanelGatewayClient() {
		return jPanelGatewayClient;
	}

	public JButton getEnableBefore() {
		return enableBefore;
	}

	public JButton getEnableAfter() {
		return enableAfter;
	}

	public JTextField getServer_serverInputPort() {
		return server_serverInputPort;
	}

	public JTextField getSlaveDeleteServerPort_before() {
		return slaveDeleteServerPort_before;
	}

	public JTextField getServer_serverIP() {
		return server_serverIP;
	}

	public JTextField getBeforeMulticastIP() {
		return beforeMulticastIP;
	}

	public JTextField getBeforeMulticastPort() {
		return beforeMulticastPort;
	}

	public JTextField getMasterJobPort() {
		return masterJobPort;
	}

	public JTextField getSlaveIP() {
		return slaveIP;
	}

	public JTextField getInquiryMaxTime() {
		return inquiryMaxTime;
	}

	public JTextField getInquiryMaxPacket() {
		return inquiryMaxPacket;
	}

	public JTextField getSlaveMulticastIP() {
		return slaveMulticastIP;
	}

	public JTextField getSlaveMulticastPort() {
		return slaveMulticastPort;
	}

	public JTextField getClient_serverInputPort() {
		return client_serverInputPort;
	}

	public JTextField getSlaveDeleteServerPort_after() {
		return slaveDeleteServerPort_after;
	}

	public JTextField getClient_serverIP() {
		return client_serverIP;
	}

	public JTextField getAfterMulticastIP() {
		return afterMulticastIP;
	}

	public JTextField getAfterMulticastPort() {
		return afterMulticastPort;
	}

	public void disableGatewayClient() {
		getClient_serverInputPort().setEnabled(false);
		getSlaveDeleteServerPort_before().setEnabled(false);
		getServer_serverIP().setEnabled(false);
		getBeforeMulticastIP().setEnabled(false);
		getBeforeMulticastPort().setEnabled(false);
		getJLabel20().setText("DISABLED");
		getJLabel20().setForeground(new java.awt.Color(255,0,0));
	}

	public void enableGatewayClient() {
		getClient_serverInputPort().setEnabled(true);
		getSlaveDeleteServerPort_before().setEnabled(true);
		getServer_serverIP().setEnabled(true);
		getBeforeMulticastIP().setEnabled(true);
		getBeforeMulticastPort().setEnabled(true);
		getJLabel20().setText("ENABLED");
		getJLabel20().setForeground(new java.awt.Color(2,255,0));		
	}

	private void enableBeforeMouseClicked(MouseEvent evt) {
		if (gatewayClientEnabled==true) {
			gatewayClientEnabled=false;
			disableGatewayClient();
			jLabel1.setEnabled(false);
		} else {
			gatewayClientEnabled=true;
			enableGatewayClient();
			jLabel1.setEnabled(true);
		}
	}

	public void disableGatewayServer() {
		getServer_serverInputPort().setEnabled(false);
		getSlaveDeleteServerPort_after().setEnabled(false);
		getClient_serverIP().setEnabled(false);
		getAfterMulticastIP().setEnabled(false);
		getAfterMulticastPort().setEnabled(false);
		getJLabel21().setText("DISABLED");
		getJLabel21().setForeground(new java.awt.Color(255,0,0));
	}

	public void enableGatewayServer() {
		getServer_serverInputPort().setEnabled(true);
		getSlaveDeleteServerPort_after().setEnabled(true);
		getClient_serverIP().setEnabled(true);
		getAfterMulticastIP().setEnabled(true);
		getAfterMulticastPort().setEnabled(true);
		getJLabel21().setText("ENABLED");
		getJLabel21().setForeground(new java.awt.Color(2,255,0));
	}

	private void enableAfterMouseClicked(MouseEvent evt) {
		if (gatewayServerEnabled==true) {
			gatewayServerEnabled=false;
			disableGatewayServer();
			jLabel3.setEnabled(false);
		} else {
			gatewayServerEnabled=true;
			enableGatewayServer();
			jLabel3.setEnabled(true);
		}

	}
	
	
	/**
	 * Evento pressione pulsante START
	 * @param Mouse event evt
	 */
	private void jButtonStartMouseClicked(MouseEvent evt) {
	
		/**
		 * Recupero tutti i dati dai jTextField e li assegno alle variabili del Master
		 */

		//BeforeGatewayLink
		getMaster().setClient_ServerInputPort(Integer.parseInt(getClient_serverInputPort().getText()));
		getMaster().setSlaveDeleteServerPort_before(Integer.parseInt(getSlaveDeleteServerPort_before().getText()));
		try {
			getMaster().setClient_ServerIP(InetAddress.getByName(getClient_serverIP().getText()));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		try {
			getMaster().setBeforeMulticastIP(InetAddress.getByName(getBeforeMulticastIP().getText()));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		getMaster().setBeforeMulticastPort(Integer.parseInt(getBeforeMulticastPort().getText()));

		//Master Gateway-Job
		getMaster().setEnableBefore(gatewayClientEnabled);
		getMaster().setEnableAfter(gatewayServerEnabled);
		getMaster().setDefaultFileName(getJTextField_DefaultFile().getText());
		getMaster().setMasterJobPort(Integer.parseInt(getMasterJobPort().getText()));
		try {
			getMaster().setSlaveIP(InetAddress.getByName(getSlaveIP().getText()));
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		}
		getMaster().setInquiryMaxTime(Integer.parseInt(getInquiryMaxTime().getText()));
		getMaster().setInquiryMaxPacket(Integer.parseInt(getInquiryMaxPacket().getText()));
		getMaster().setInquiryThresholding(Integer.parseInt(getJTextField_InquiryThresholding().getText()));
		try {
			getMaster().setSlaveMulticastIP(InetAddress.getByName(getSlaveMulticastIP().getText()));
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		}
		getMaster().setSlaveMulticastPort(Integer.parseInt(getSlaveMulticastPort().getText()));
		getMaster().setNetworkInterface(getJComboBox_NetworkInterfaces().getSelectedItem().toString());
		
		getMaster().setHciDev(getJComboBox_BluetoothInterfaces().getSelectedIndex());


		//Gateway Server
		getMaster().setServer_ServerInputPort(Integer.parseInt(getServer_serverInputPort().getText()));
		getMaster().setSlaveDeleteServerPort_after(Integer.parseInt(getSlaveDeleteServerPort_after().getText()));
		try {
			getMaster().setServer_ServerIP(InetAddress.getByName(getServer_serverIP().getText()));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		try {
			getMaster().setAfterMulticastIP(InetAddress.getByName(getAfterMulticastIP().getText()));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		getMaster().setAfterMulticastPort(Integer.parseInt(getAfterMulticastPort().getText()));
		
		if (getJCheckReset().isSelected()) masterReset();

		/**
		 * Se sono stati DISABILITATI uno o più link con i vicini, copio in ./gatewayTmp il relativo xml "finto"
		 */
//		if (gatewayClientEnabled==false) {
//			File getBeforeXml = new File("./master_reset/before_gateway.xml");
//			File writeBeforeXml = new File("./gatewayTmp/before_gateway.xml");
//			try {
//				copyFile(getBeforeXml, writeBeforeXml);
//			} catch (IOException ioe) {
//				ioe.printStackTrace();
//			}
//		}
//
//		if (gatewayServerEnabled==false) {
//			File getAfterXml = new File("./master_reset/after_gateway.xml");
//			File writeAfterXml = new File("./gatewayTmp/after_gateway.xml");
//			try {
//				copyFile(getAfterXml,writeAfterXml);
//			} catch (IOException i) {
//				i.printStackTrace();
//			}
//		}

		this.setVisible(false);
		this.dispose();
		synchronized (getMaster()) {
			getMaster().notify();
		}
	}

	public JLabel getJLabel20() {
		return jLabel20;
	}

	public JLabel getJLabel21() {
		return jLabel21;
	}

	/**
	 * Metodo per la copia a byte di un file
	 * @param src Path completo del file sorgente
	 * @param dst Path completo del file destinazione
	 * @throws IOException
	 */
	public void copyFile(File src, File dst) throws IOException {

		InputStream in = new FileInputStream(src);		   
		OutputStream out = new FileOutputStream(dst);

		byte[] buf = new byte[1024];
		int len;
		while ((len = in.read(buf)) > 0) {
			out.write(buf, 0, len);
		}
		in.close();
		out.close();
	}

	/**
	 * Metodo che copia una directory e tutti i suoi sotto file e sotto directory in un'altra
	 * @param sDir Path completo della directory sorgente
	 * @param dDir Patch completo della directory di uscita
	 * @throws IOException
	 */
	public void copyDirectory(String sDir, String dDir) throws IOException{
		File srcDir = new File(sDir);
		File dstDir = new File(dDir);
		if (srcDir.isDirectory()) {
			if (!dstDir.exists()) {
				dstDir.mkdir();
			}
			String[] children = srcDir.list();
			for (int i=0; i<children.length; i++) {
				if (!children[i].contains("svn")) {
					copyDirectory(sDir+"/"+children[i], dDir+"/"+children[i]);
				}
			}
		} else {
			copyFile(srcDir, dstDir);
		}
	}

	/**
	 * Metodo che elimina i file contenuti nella directory fornita in ingresso
	 * @param srcDir Path completo della directory da svuotare
	 * @throws IOException
	 */
	public void cleanDir(String srcDir) throws IOException {
		File dir = new File(srcDir);
		if (dir.isDirectory()) {
			if (dir.exists()) {
				String[] children = dir.list();
				for (int i=0; i<children.length; i++) {
					File toDelete = new File(dir+"/"+children[i]);
					toDelete.delete();
				}
			}
		}
	}
	
	/**
	 * Metodo che resetta i file locali utilizzati 
	 */
	private void masterReset() {
		System.err.println("Resetto i file locali...");
		
		//svuoto la cartella gatewayTmp
		try {
			cleanDir("./gatewayTmp");
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		//copio i file che servono all'inizio all'interno di gatewayTmp
		try {
			copyDirectory("./master_reset/gatewayTmp", "./gatewayTmp");
		} catch (IOException e) {
			e.printStackTrace();
		}

		//controllo se slaveIP = localhost. In quel caso copio anche slaveTmp e Xml
		if (getSlaveIP().getText().equalsIgnoreCase("localhost")) {
			//svuoto la cartella slaveTmp
			try {
				cleanDir("./slaveTmp");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			//copio i file che servono all'inizio all'interno di slaveTmp
			try {
				copyDirectory("./master_reset/slaveTmp", "./slaveTmp");
			} catch (IOException e) {
				e.printStackTrace();
			}
			//svuoto la cartella xml
			try {
				cleanDir("./xml");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			//copio i file che servono all'inizio all'interno di xml
			try {
				copyDirectory("./master_reset/xml", "./xml");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} 
	}

	
	public JCheckBox getJCheckReset() {
		return jCheckReset;
	}
	
	public JComboBox getJComboBox_NetworkInterfaces() {
		if (jComboBox_NetworkInterfaces==null) {
			Vector<String>interf=new Vector<String>();
			NetworkInterfaceIP net = new NetworkInterfaceIP();
			Enumeration<NetworkInterface> interfaces = net.getAvailableNetworkInterfaces();
			while (interfaces.hasMoreElements()) {
				NetworkInterface nif = interfaces.nextElement();
				interf.add(nif.getDisplayName());
			}
			
			jComboBox_NetworkInterfaces = new JComboBox(interf);
			jComboBox_NetworkInterfaces.setSelectedIndex(0);
			jComboBox_NetworkInterfaces.setPreferredSize(new java.awt.Dimension(84, 23));
		}
		return jComboBox_NetworkInterfaces;
	}
	
	private JLabel getJLabel22() {
		if(jLabel22 == null) {
			jLabel22 = new JLabel();
			jLabel22.setText("Network interface");
			jLabel22.setPreferredSize(new java.awt.Dimension(111, 23));
		}
		return jLabel22;
	}

	/**
	 * Metodo che restituisce un array di String contenente i nomi delle interfacce hci Bluetooth disponibili
	 * @return String[]
	 */
	private String[] getBluetoothInterfaces()
	{
		Vector<?> vector = null;
		
		try
		{
			vector = BlueCoveImpl.getLocalDevicesID();
		} 
		catch (BluetoothStateException e)
		{
			e.printStackTrace();
		}
		
		BlueCoveImpl.shutdown();
		
		if(vector != null)
		{
			int size = vector.size();
			String[] deviceIDstringArray = new String[size];
			
			int i;
			for(i=0; i<size; i++)
			{
				deviceIDstringArray[i] = vector.get(i).toString();
			}

			return deviceIDstringArray;
		}
		else
		{
			return null;
		}
	}
	
	private JLabel getJLabel23() {
		if(jLabel23 == null) {
			jLabel23 = new JLabel();
			jLabel23.setText("Bluetooth interface");
		}
		return jLabel23;
	}
	
	public JComboBox getJComboBox_BluetoothInterfaces() {
		if(jComboBox_BluetoothInterfaces == null) {
			ComboBoxModel jComboBox_BluetoothInterfacesModel = 
				new DefaultComboBoxModel(
						getBluetoothInterfaces());
			jComboBox_BluetoothInterfaces = new JComboBox();
			jComboBox_BluetoothInterfaces.setModel(jComboBox_BluetoothInterfacesModel);
			jComboBox_BluetoothInterfaces.setSelectedIndex(0);
		}
		return jComboBox_BluetoothInterfaces;
	}
	
	private JLabel getJLabel24() {
		if(jLabel24 == null) {
			jLabel24 = new JLabel();
			jLabel24.setPreferredSize(new java.awt.Dimension(201, 7));
		}
		return jLabel24;
	}
	
	private JLabel getJLabel25() {
		if(jLabel25 == null) {
			jLabel25 = new JLabel();
			jLabel25.setPreferredSize(new java.awt.Dimension(201, 7));
		}
		return jLabel25;
	}
	
	public JLabel getJLabel26() {
		if(jLabel26 == null) {
			jLabel26 = new JLabel();
			jLabel26.setText("Inquiry RSSI Thresholding");
		}
		return jLabel26;
	}
	
	public JTextField getJTextField_InquiryThresholding() {
		if(jTextField_InquiryThresholding == null) {
			jTextField_InquiryThresholding = new JTextField();
			jTextField_InquiryThresholding.setText("78");
		}
		return jTextField_InquiryThresholding;
	}
	
	private JLabel getJLabel27() {
		if(jLabel27 == null) {
			jLabel27 = new JLabel();
			jLabel27.setText("Default file name");
		}
		return jLabel27;
	}
	
	public JTextField getJTextField_DefaultFile() {
		if(jTextField_DefaultFile == null) {
			jTextField_DefaultFile = new JTextField();
			jTextField_DefaultFile.setText("file2");
		}
		return jTextField_DefaultFile;
	}
	
	private JLabel getJLabel28() {
		if(jLabel28 == null) {
			jLabel28 = new JLabel();
			jLabel28.setPreferredSize(new java.awt.Dimension(109, 10));
		}
		return jLabel28;
	}
	
	private JLabel getJLabel29() {
		if(jLabel29 == null) {
			jLabel29 = new JLabel();
			jLabel29.setPreferredSize(new java.awt.Dimension(109, 10));
		}
		return jLabel29;
	}

}
